package com.gamewithpurpose.dbinterface;

import java.io.File;

import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;

/*
 * DBFactory initiates a hibernate with a configuration. 
 */
public class DBFactory {
	private static SessionFactory sessionFactory;
	private static String cfgPath = "hibernate.cfg.xml";

	/*
	 * Set a hibernate configuration file path.
	 */
	public static void setCfgPath(final String path) {
		cfgPath = path;
	}

	/*
	 * Get a session factory instance.
	 */
	public static SessionFactory getSessionFactory() {
		if (cfgPath == null) {
			throw new ExceptionInInitializerError("cfg path is null");
		}

		try {
			sessionFactory = new Configuration().configure(cfgPath).buildSessionFactory();
		} catch (Throwable ex) {
			System.err.println("Failed to create sessionFactory object." + ex);
			throw new ExceptionInInitializerError(ex);
		}

		return sessionFactory;
	}
}
